﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ProgNetComponents.Ini;
using ProgNetComponentsDx.Layout;
using ProgNetComponentsDx.Options;
using DevExpress.XtraEditors;
using ProgNetComponents.Data.SqlCe;
using Wydatki.Setup.Lotto;
using Wydatki.Tools.Lotto;
using ProgNetComponentsDx.Browsers;

namespace Wydatki.Init
{
    public partial class InitForm : ProgNetComponentsDx.Forms.BaseInitForm
    {
        public InitForm()
        {
            InitializeComponent();
        }
        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
        }
        
        protected override void PreInit()
        {
            base.PreInit();
            BrowserCache.SaveLayouts = true;
            SetProgressText("Inicjowanie bazy danych.");
            IniFile iniFile = new IniFile(Application.StartupPath + "\\Wydatki.ini");
            iniFile.ReadIni();
            string dbFile = iniFile.GetValueFormCache("Main", "DB");
            if (dbFile.IsNullOrEmpty())
                dbFile = "Wydatki.sdf";
            ProgNetComponents.Data.SqlCe.SqlCeDataManager.GlobalConnectionString = @"Data Source=" + dbFile + "; Encrypt Database=False; Max Database Size = 4091"; //File Mode=Read Write;
            ProgNetComponents.Data.SqlCe.SqlCeDataManager helper = new ProgNetComponents.Data.SqlCe.SqlCeDataManager();
            
            int lastAction = 0;
            if (System.IO.File.Exists(dbFile))
            {
                try
                {
                    object obj = helper.ExecuteScalar("SELECT STP_OP_WARTOSC FROM STP_OPCJE WHERE STP_OP_NAZWA = 'DB_UPDATE'");
                    if (obj != null)
                    {
                        lastAction = obj.AsInt();
                    }
                }
                catch { }
            }
            AppUpdate.Update(lastAction);
        }
        protected override ProgNetComponentsDx.Data.SelectDataManagerCreator CreateDataManagerCreator()
        {
            return new ProgNetComponentsDx.Data.SqlCeSelectDataManagerCreator();
        }
        protected override ProgNetComponents.Data.DataManager CreateDataManagerForBrowsers()
        {
            return new SqlCeDataManager();
        }
        protected override LayoutGrabber CreateLayoutGrabber()
        {
            SqlCeLayoutGrabber g = new SqlCeLayoutGrabber();
            g.TableName = "STP_LAYOUT";
            g.XmlColumn = "STP_LAYOUT_XML_IMG";
            g.LayoutNameColumn = "STP_LAYOUT_NAZWA";
            g.ControlNameColumn = "STP_LAYOUT_KONTROLKA";
            return g;
        }
        protected override OptionGrabber CreateOptionGrabber()
        {
            SqlCeOptionGrabber g = new SqlCeOptionGrabber();
            g.TableName = "STP_OPCJE";
            g.OptionNameColumn = "STP_OP_NAZWA";
            g.OptionValueColumn = "STP_OP_WARTOSC";
            return g;
        }
        protected override void InitOptions()
        {
            base.InitOptions();
            OptionCache.GetOption(InitFormOptionNames.Rozne.Skorka).DefaultValue = this.LookAndFeel.ActiveSkinName;
            OptionCache.GetOption(InitFormOptionNames.Rozne.Styl).DefaultValue = this.LookAndFeel.Style;
            ProgNetComponentsDx.Forms.FormSkinChooser.ApplayStyle(OptionCache.GetOptionValue(InitFormOptionNames.Rozne.Styl).AsString(),
                OptionCache.GetOptionValue(InitFormOptionNames.Rozne.Skorka).AsString());
        }
        protected override ProgNetComponentsDx.Shortcut.ShortcutGrabber CreateShotrcutsGrabber()
        {
            ProgNetComponentsDx.Shortcut.SqlCeShortcutGrabber g = new ProgNetComponentsDx.Shortcut.SqlCeShortcutGrabber();
            g.ShortcutNameColumn = "STP_SC_NAZWA";
            g.ShortcutValueColumn = "STP_SC_WARTOSC";
            g.TableName = "STP_SHORTCUT";
            return g;
        }
        #region Browsers Static Data
        protected override DataTable BrowserCache_GetStaticData(object browserId)
        {
            DataTable dt = null;
            if (BrowserCache.IsTheSameBrowser(browserId, InitFormBrowserNames.Glowna.LottoTypStatystykWoboru))
            {
                dt = ActionLottoBase.GetLottoTypesForWybor();
            }
            else if (BrowserCache.IsTheSameBrowser(browserId, InitFormBrowserNames.Glowna.LottoTypyStatystykLosowan))
            {
                dt = ActionLottoBase.GetLottoTypesForLosowania();
            }
            else if (BrowserCache.IsTheSameBrowser(browserId, InitFormBrowserNames.Glowna.Miesiace))
            {
                dt = GetMiesiace();
            }
            else if (BrowserCache.IsTheSameBrowser(browserId, InitFormBrowserNames.Glowna.LottoTypyStatystykGlobalnych))
            {
                dt = ActionLottoBase.GetGlobalLottoTypes();
            }
            else if (BrowserCache.IsTheSameBrowser(browserId, InitFormBrowserNames.Glowna.LottoTypWyboru))
            {
                dt = GetLottoTypyWyboru();
            }
            return dt;
        }

        private DataTable GetLottoTypyWyboru()
        {
            DataTable dt = GetStaticTable(Init.InitFormBrowserNames.Glowna.LottoTypWyboru);
            dt.Rows.Add("W", "Wybierz");
            dt.Rows.Add("P", "Pomiń");
            return dt;
        }

        private DataTable GetMiesiace()
        {
            DataTable dt = GetStaticTable(InitFormBrowserNames.Glowna.Miesiace);
            for (int i=1; i < 13; i++)
                dt.Rows.Add(i, System.Globalization.CultureInfo.CurrentCulture.DateTimeFormat.GetMonthName(i));
            return dt;
        }

        public static DataTable GetStaticTable(object browserId)
        {
            DataTable dt = new DataTable(browserId.ToString());
            dt.Columns.Add("KOD");
            dt.Columns.Add("NAZWA");
            return dt;
        }

        #endregion

        protected override void InitLangouage()
        {
            base.InitLangouage();
            ProgNetComponentsDx.Language.ProgNetLanguage pl = new ProgNetComponentsDx.Language.ProgNetLanguage(new System.Globalization.CultureInfo("pl-PL"));
            pl.RegisterManager("DB", LNG.DB.ResourceManager);
            pl.RegisterManager("Messages", LNG.Messages.ResourceManager);
            ProgNetComponentsDx.Language.ProgNetLanguage.LogMissingValues = true;
            ProgNetComponentsDx.Language.ProgNetLanguage.CurrentLanguageCode = "pl-PL";
        }
    }
}
